package qdu.rg012.ch4_data_contntrsvr;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.CursorIndexOutOfBoundsException;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Myrsvr extends Activity {
    ContentResolver cr;
    TextView tvshowmsg;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ly_myrsvr);
        cr = this.getContentResolver();
        tvshowmsg = (TextView)findViewById(R.id.tv_showmsg);
        findViewById(R.id.btn_insert).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String strtmp = "";
                Uri newuri = null;
                ContentValues cv = new ContentValues();
                strtmp = ((EditText)findViewById(R.id.et_msg)).getEditableText().toString();
                Log.i("mytag", "strtmp: " +strtmp + " trim:" + strtmp.trim());
                if(strtmp.hashCode() != 0 && !"".equals(strtmp.trim())) {
                    cv.put(Profile.KEY_DATA, strtmp);
                    if(cr != null)
                        newuri = cr.insert(Uri.parse(Profile.uristr + Profile.multi_path), cv);
                    Toast.makeText(Myrsvr.this, newuri.toString(), Toast.LENGTH_SHORT).show();
                }else
                    Toast.makeText(Myrsvr.this, "do not leave it empty", Toast.LENGTH_SHORT).show();
            }
        });

        findViewById(R.id.btn_getdata).setOnClickListener(new View.OnClickListener() {
            @SuppressLint("Range")
            @Override
            public void onClick(View v) {
                String id = "";
                Cursor c = null;
                id = ((EditText)findViewById(R.id.et_id)).getEditableText().toString();
                if(id.hashCode() != 0 && !"".equals(id.trim())){
                    if(cr != null) {
                        c = cr.query(Uri.parse(Profile.uristr + Profile.item_path), null, id, null, null);
                        if(c != null) {
                            c.moveToFirst();
                            String tmp = "";
                            try {
                                tmp = c.getString(c.getColumnIndex(Profile.KEY_DATA));
                            }catch (CursorIndexOutOfBoundsException e){
                                Toast.makeText(Myrsvr.this, "不存在当前id", Toast.LENGTH_SHORT).show();
                                //e.printStackTrace();
                            }catch(Exception e){
                                Toast.makeText(Myrsvr.this, "取值失败", Toast.LENGTH_LONG).show();
                            }

                            tvshowmsg.setText(tmp);
                            c.close();
                        }
                    }
                }else{
                    Cursor call = null;
                    if(cr != null) {
                        c = cr.query(Uri.parse(Profile.uristr + Profile.multi_path), null, null, null, null);
                        if(c != null) {
                            c.moveToFirst();
                            String tmp = "";
                            for(int i = 0; i< c.getCount(); i++){
                                try {
                                    tmp += c.getString(c.getColumnIndex(Profile.KEY_ID)) + " : " +
                                            c.getString(c.getColumnIndex(Profile.KEY_DATA)) + '\n';
                                }catch (CursorIndexOutOfBoundsException e){
                                    Toast.makeText(Myrsvr.this, "不存在当前id", Toast.LENGTH_SHORT).show();
                                    //e.printStackTrace();
                                }catch(Exception e){
                                    Toast.makeText(Myrsvr.this, "取值失败", Toast.LENGTH_LONG).show();
                                }
                                if(!c.moveToNext())
                                    break;
                            }
                            tvshowmsg.setText(tmp);
                            c.close();//resolver中提倡主动close使用完的cursor
                        }
                    }
                }
            }
        });
    }
}
